package org.esprit.ocm.client.ui.ec2;

import java.util.List;

import org.esprit.ocm.client.model.ec2.ImageRecord;
import org.esprit.ocm.client.model.ec2.datasource.ImageEC2LocalDS;
import org.esprit.ocm.client.rpc.ec2.ImageService;
import org.esprit.ocm.client.rpc.ec2.ImageServiceAsync;
import org.esprit.ocm.client.rpc.ec2.RegistrationService;
import org.esprit.ocm.client.rpc.ec2.RegistrationServiceAsync;
import org.esprit.ocm.dto.ec2.impl.ImageDescriptionDto;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.viewer.DetailViewer;

public class ImageZone extends SectionStack {

	private final ImageServiceAsync			imageService		= GWT.create(ImageService.class);
	private final RegistrationServiceAsync	registrationService	= GWT.create(RegistrationService.class);

	private DataSource						ds;
	private ListGrid						grid;
	private DetailViewer					details;

	public ImageZone(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		getData(server, credentials);

		// creer imageSection
		SectionStackSection imageSection = new SectionStackSection("Image List");
		imageSection.setExpanded(true);
		addSection(imageSection);

		// creer imageGrid
		grid = new ListGrid();
		setVisibilityMode(VisibilityMode.MULTIPLE);
		setHeaderHeight(20);
		// instanceGrid.setShowFilterEditor(true);
		grid.setFilterOnKeypress(true);
		grid.setAutoFetchData(true);

		// creer imagetoolstrip
		ToolStrip imageGridToolStrip = new ToolStrip();
		imageGridToolStrip.setWidth100();
		imageGridToolStrip.addFill();

		// ajouter toolstrip a instanceSection
		imageSection.addItem(imageGridToolStrip);

		Img filter = new Img("filter.png", 15, 15);

		// creer filterButton
		final Button filterButtonOn = new Button("On");
		filterButtonOn.setWidth(25);
		filterButtonOn.setHeight(20);
		filterButtonOn.setShowRollOver(true);
		filterButtonOn.setShowDown(true);
		// filterButtonOn.setIcon("filter.png");
		filterButtonOn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(true);
				grid.setFilterOnKeypress(true);
			}
		});
		// creer filterButton
		final Button filterButtonOff = new Button("Off");
		filterButtonOff.setWidth(25);
		filterButtonOff.setHeight(20);
		filterButtonOff.setShowRollOver(true);
		filterButtonOff.setShowDown(true);
		// filterButtonOff.setIcon("filter.png");
		filterButtonOff.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(false);
				grid.setFilterOnKeypress(false);
			}
		});

		final Button runButton = new Button("Run Instance");
		runButton.setShowRollOver(true);
		runButton.setShowDown(true);
		runButton.disable();
		runButton.setIcon("play.png");
		runButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ImageRecord ir = (ImageRecord) grid.getSelectedRecord();
				RunInstance runInstance = new RunInstance(ir, server, credentials, user);
				runInstance.show();
			}
		});

		final Button registerButton = new Button("Register");
		registerButton.setShowRollOver(true);
		registerButton.setShowDown(true);
		registerButton.setIcon("add.png");
		registerButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RegisterImage registerImage = new RegisterImage(server, credentials, user);
				registerImage.show();
			}
		});

		final Button refreshButton = new Button("Refresh");
		refreshButton.setShowRollOver(true);
		refreshButton.setShowDown(true);
		refreshButton.setIcon("interact.png");
		refreshButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Refreshed");
				ds.destroy();
				getData(server, credentials);
				grid.redraw();
			}
		});

		final Button deleteButton = new Button("Deregister");
		deleteButton.setShowRollOver(true);
		deleteButton.setShowDown(true);
		deleteButton.setIcon("remove.png");
		deleteButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String imageId = grid.getSelectedRecord().getAttribute("id");
				registrationService.deregisterImage(imageId, server, credentials, new AsyncCallback<Boolean>() {
					
					public void onSuccess(Boolean rep) {

						if(rep){
							SC.say("Deleted");
							ds.destroy();
							getData(server, credentials);
							grid.redraw();
						}else{
							SC.warn("error");
						}
					}
					
					public void onFailure(Throwable arg0) {
						
					}
				});
			}
		});

		// ajouter le bouton
		imageGridToolStrip.addMember(runButton);
		imageGridToolStrip.addMember(registerButton);
		imageGridToolStrip.addMember(deleteButton);
		imageGridToolStrip.addMember(refreshButton);
		imageGridToolStrip.addSpacer(10);
		imageGridToolStrip.addMember(filter);
		imageGridToolStrip.addSpacer(2);
		imageGridToolStrip.addMember(filterButtonOn);
		imageGridToolStrip.addMember(filterButtonOff);
		imageGridToolStrip.addSpacer(5);
		
		// ajouter instanceGrid a instanceSection
		imageSection.addItem(grid);

		// creer DetailinstanceSection

		details = new DetailViewer();
		details.setWidth100();
		details.setMargin(15);
		details.setEmptyMessage("Select something to view its details");

		grid.addRecordClickHandler(new RecordClickHandler() {

			public void onRecordClick(RecordClickEvent event) {

				details.setData(new Record[] { event.getRecord() });
				
				if (event.getRecord().getAttribute("imageType").toLowerCase().equals("machine")) {
					
					runButton.enable();
				}else {
					runButton.disable();
				}
			}
		});

		SectionStackSection imageDetailSection = new SectionStackSection("Image Detail");
		imageDetailSection.setExpanded(true);
		imageDetailSection.addItem(details);
		addSection(imageDetailSection);

		draw();
	}

	private void getData(ServerDto server, AwsCredentialsDto credentials) {
		imageService.listImages(true, server, credentials, new AsyncCallback<List<ImageDescriptionDto>>() {

			public void onSuccess(List<ImageDescriptionDto> list) {

				for (ImageDescriptionDto i : list) {
					System.out.println(i.getImageId());
				}
				ds = ImageEC2LocalDS.getInstance(list);
				grid.setDataSource(ds);
				details.setDataSource(ds);
				
				ListGridField idField = new ListGridField("id");
				ListGridField manifestField = new ListGridField("manifest", 400);
				ListGridField stateField = new ListGridField("state");
				ListGridField ownerField = new ListGridField("owner");
				ListGridField visibilityField = new ListGridField("visibility");
				ListGridField architectureField = new ListGridField("architecture");
		
				grid.setFields(idField, manifestField, stateField, ownerField, visibilityField, architectureField);

			}

			public void onFailure(Throwable arg0) {

			}
		});
	}

}
